package homewoek12月19号.homwwork.h4;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Element root = new SAXReader().read("src/com/czxy/homwwork/h4/h5.xml").getRootElement();
        List<Element> elist = root.elements("bean");
        for (Element e : elist) {
            HashMap<String, Object> map = new HashMap<>();
            String s = e.attributeValue("className");
            Object o = Class.forName(s).newInstance();
            List<Element> plist = e.elements("property");
            for (Element p : plist) {
                map.put(p.attributeValue("name"),p.attributeValue("value"));
            }
            DateConverter dateConverter = new DateConverter();
            dateConverter.setPatterns(new String[]{"yyyy-MM-dd"});
            ConvertUtils.register(dateConverter, Date.class);
            BeanUtils.populate(o,map);
            System.out.println(o);
        }
    }
}
